package license

import (
	"context"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/beherochuling/beesoft_api/app/dao"
	"github.com/beherochuling/beesoft_api/app/model/entity"
)

func NewLicenseCode() *LicenseCodeService {
	return &LicenseCodeService{}
}

type LicenseCodeService struct {
	transactionId string
	activateType  uint
}

func (r *LicenseCodeService) ScanBatch(ctx context.Context, callback func(model *gdb.Model) *gdb.Model) *entity.VipLicenses {
	var item *entity.VipLicenses

	model := dao.VipLicenses.Ctx(ctx)

	model = callback(model)

	err := model.Scan(&item)
	if err != nil {
		panic(err)
	}

	return item
}
func (r *LicenseCodeService) ScanRights(ctx context.Context, callback func(model *gdb.Model) *gdb.Model) []entity.VipLicenseRights {
	var items []entity.VipLicenseRights

	model := dao.VipLicenseRights.Ctx(ctx)

	model = callback(model)

	err := model.Scan(&items)
	if err != nil {
		panic(err)
	}

	return items
}
func (r *LicenseCodeService) ScanCode(ctx context.Context, callback func(model *gdb.Model) *gdb.Model) *entity.VipLicenseCodes {
	var item *entity.VipLicenseCodes

	model := dao.VipLicenseCodes.Ctx(ctx)

	model = callback(model)

	err := model.Scan(&item)
	if err != nil {
		panic(err)
	}

	return item
}
func (r *LicenseCodeService) UpdateCodeInner(ctx context.Context, callback func(model *gdb.Model) *gdb.Model, data g.Map) {
	model := dao.VipLicenseCodes.Ctx(ctx)

	model = callback(model)

	_, err := model.Update(data)
	if err != nil {
		panic(err)
	}
}

func (r *LicenseCodeService) ShowBatch(ctx context.Context, batchId uint) *entity.VipLicenses {
	return r.ScanBatch(ctx, func(model *gdb.Model) *gdb.Model {
		return model.WherePri(batchId)
	})
}
func (r *LicenseCodeService) ShowRights(ctx context.Context, batchId uint) []entity.VipLicenseRights {
	return r.ScanRights(ctx, func(model *gdb.Model) *gdb.Model {
		return model.Where(dao.VipLicenseRights.Columns().LicenseId, batchId)
	})
}
func (r *LicenseCodeService) ShowCode(ctx context.Context, code string) *entity.VipLicenseCodes {
	return r.ScanCode(ctx, func(model *gdb.Model) *gdb.Model {
		return model.Where(dao.VipLicenseCodes.Columns().Code, code)
	})
}
func (r *LicenseCodeService) UpdateCode(ctx context.Context, id uint, data g.Map) {
	r.UpdateCodeInner(ctx, func(model *gdb.Model) *gdb.Model {
		return model.WherePri(id)
	}, data)
}
func (r *LicenseCodeService) DestroyBatchByTransactionId(ctx context.Context, transactionId string) {
	batch := r.ScanBatch(ctx, func(model *gdb.Model) *gdb.Model {
		return model.Where(dao.VipLicenses.Columns().TransactionId, transactionId)
	})
	if batch != nil {
		// 删除
		_, err := dao.VipLicenses.Ctx(ctx).
			WherePri(batch.Id).
			Delete()
		if err != nil {
			panic(err)
		}

		// todo 可能有多个 code
		code := r.ScanCode(ctx, func(model *gdb.Model) *gdb.Model {
			return model.Where(dao.VipLicenseCodes.Columns().LicenseId, batch.Id)
		})
		if code != nil && code.CountActived > 0 {
			License.Reset(ctx, code)
		}
	}
}
